package lucene.server.web.controller.admin;


import lombok.extern.slf4j.Slf4j;
import lucene.server.cdc.CDCTask;
import lucene.server.common.model.Result;
import lucene.server.common.utils.StringUtil;
import lucene.server.web.dao.SystemDao;
import lucene.server.web.model.CDCConfig;
import lucene.server.web.task.DataChangeTask;
import lucene.server.web.task.TaskPool;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.UUID;


@RestController
@RequestMapping("/admin/cdc")
@Slf4j
public class CDCController {

    @GetMapping(value = "/get")
    public Result<?> get(HttpServletRequest req) throws SQLException {
        return  Result.OK( SystemDao.getAll("cdc", CDCConfig.class));
    }
    @GetMapping(value = "/getStatus")
    public Result<?> getStatus(HttpServletRequest req)  {

        return  Result.OK( );
    }
    @PostMapping(value = "/reStart")
    public Result<?> reStart(@RequestBody CDCConfig cdcConfig)  {
        TaskPool.shutdown("cdc_"+cdcConfig.getName());
        CDCTask canalTask = new CDCTask(cdcConfig.toProperties());
        TaskPool.addTask(canalTask);
        return  Result.OK();
    }
    @PostMapping(value = "/update")
    public Result<?> update(@RequestBody CDCConfig cdcConfig, HttpServletRequest req) {
        System.out.println(cdcConfig.toProperties());
        //如果id没有，先判断是否存在
        if (StringUtil.isEmpty(cdcConfig.getId()) && SystemDao.checkExists(cdcConfig, "name")) {
            return Result.error("CDC名称已存在");
        }
        if (StringUtil.isEmpty(cdcConfig.getId())){
            cdcConfig.setId( UUID.randomUUID().toString().replace("-", ""));
        }else{
            CDCConfig oldCDCConfig = SystemDao.findOne(cdcConfig, CDCConfig.class);
            TaskPool.shutdown("cdc_"+oldCDCConfig.getName());
        }
        String error = checkCDC(cdcConfig);
        if (!StringUtil.isEmpty(error)){
            return Result.error(error);
        }
        SystemDao.insertOrUpdate(cdcConfig);
        CDCTask canalTask = new CDCTask(cdcConfig.toProperties());
        TaskPool.addTask(canalTask);
        return Result.OK();
    }

    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestBody CDCConfig cdcConfig, HttpServletRequest req) {
        SystemDao.delete(cdcConfig);
        TaskPool.shutdown("cdc_"+cdcConfig.getName());
        return Result.OK();
    }
    private String checkCDC(CDCConfig cdcConfig){
        String error = "";
        error += StringUtil.assertNull(cdcConfig.getName(),"名称不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getFilter() ,"过滤器不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getHost(),"Host不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getPort() ,"端口不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getUsername(),"用户名不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getPassword() ,"密码不能为空\n");
        error += StringUtil.assertNull(cdcConfig.getType() ,"类型不能为空\n");
        return error;
    }

}

